package Client;

import javax.swing.JOptionPane;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import BanqueGestion.Banque;
import BanqueGestion.BanqueHelper;
import BanqueGestion.CompteBancaire;

public class ClientBanque {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws CannotProceed 
	 * @throws NotFound 
	 */
	public static void main(String[] args) throws NotFound, CannotProceed, Exception {
		// Ici l'initialisation de l'ORB, etc....
		int id = 0;
		ORB orb = ORB.init(args, null);
		
		NamingContextExt context = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
		

		org.omg.CORBA.Object o1 = context.resolve_str("A");
		org.omg.CORBA.Object o2 = context.resolve_str("B");
		
		Banque banque1 = BanqueHelper.narrow(o1);
		Banque banque2 = BanqueHelper.narrow(o2);
		
		
				
		// La boucle principale
		String reponse = "";
		while ( !reponse.equals("0"))
		{
			reponse = JOptionPane.showInputDialog("1 Ajouter client dans A\n"+
													"2 Ajouter client dans B\n" +
													"3 voir client par id dans A\n"+
													"4 voir client par id dans B\n"
													,"");

			if (reponse.equals("1"))
			{
				String nom = JOptionPane.showInputDialog("Nom ?","");
				if(banque1.creerCompte(nom, ++id))
					JOptionPane.showMessageDialog(null, "id = " + id);
				else
					JOptionPane.showMessageDialog(null, "Echec");
			}
			if (reponse.equals("2"))
			{
				String nom = JOptionPane.showInputDialog("Nom ?","");

				if(banque2.creerCompte(nom, ++id))
					JOptionPane.showMessageDialog(null, "id = " + id);
				else
					JOptionPane.showMessageDialog(null, "Echec");
			}
			if (reponse.equals("3"))
			{
				int idTmp = Integer.parseInt(JOptionPane.showInputDialog("id ?",""));
				CompteBancaire compte = banque1.rechercheCompte(idTmp);
				
				if(compte != null)
					JOptionPane.showInputDialog(null, compte.nom());
				else
					JOptionPane.showMessageDialog(null, "Echec");
			}
			if (reponse.equals("4"))
			{
				int idTmp = Integer.parseInt(JOptionPane.showInputDialog("id ?",""));
				CompteBancaire compte = banque2.rechercheCompte(idTmp);
				
				if(compte != null)
					JOptionPane.showInputDialog(null, compte.nom());
				else
					JOptionPane.showMessageDialog(null, "Echec");
			}
		}
			
	}

}
